package com.yueshop.order.listener;


import com.rabbitmq.client.Channel;
import com.yueshop.common.mq.SeckillOrderTo;
import com.yueshop.order.entity.OrderEntity;
import com.yueshop.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;


@Slf4j
@RabbitListener(queues = {"order.seckill.order.queue"})
@Component
public class OrderSeckillListener {

    @Autowired
    OrderService orderService;

    @RabbitHandler
    public void hnadleOrderSeckill(SeckillOrderTo seckillOrderTo, Channel channel, Message message) throws IOException {
        try {
           log.info("装备创建秒杀订单信息........");
            //订单关闭
            orderService.saveSeckillOrder(seckillOrderTo);
            //手动调用支付宝收单功能
            channel.basicAck(message. getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            //消息拒绝后放回队列
            channel.basicReject(message. getMessageProperties().getDeliveryTag(),true);
        }
    }
}
